<template>
    <div ref="loginContainer" class="login-container">
        <el-row>
            <el-col :span="14">
                <div>欢迎使用 EasyVue 后台管理系统</div>
            </el-col>
            <el-col :span="10">
                <el-form ref="loginForm" :model="form" :rules="rules" label-width="65px" class="login-box">
                    <h3 class="login-title">欢迎登录EasyVue</h3>
                    <el-form-item label="账号：" prop="username">
                        <el-input type="text" placeholder="请输入账号" v-model="form.username"/>
                    </el-form-item>
                    <el-form-item label="密码：" prop="password">
                        <el-input type="password" placeholder="请输入密码" v-model="form.password"/>
                    </el-form-item>
                    <el-form-item style="text-align:center">
                        <el-button type="primary" v-on:click="onSubmit('loginForm')">登录</el-button>
                        <el-checkbox style="float:right;top:10px;">记住我</el-checkbox>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>
    </div>
</template>

<style>
    .login-container {
        width: 100%;
        background: url("http://demo.ruoyi.vip/img/login-background.jpg") no-repeat fixed center 0;
        -webkit-background-size: cover;
        -moz-background-size: cover;
        -o-background-size: cover;
        background-size: cover;
        color: rgba(255, 255, 255, .95);
    }

    .login-box {
        border: 1px solid #DCDFE6;
        width: 300px;
        padding: 45px 35px 5px 25px;
        border-radius: 5px;
        -webkit-border-radius: 5px;
        -moz-border-radius: 5px;
        box-shadow: 0 0 25px #909399;
        background: rgba(255, 255, 255, .2);
        margin: 20% auto 0;

    }

    .login-title {
        text-align: center;
        margin: 0 auto 40px auto;
        color: #303133;
    }
</style>
<script>
    $.define(function () {
        return {
            name: 'login',
            data() {
                return {
                    form: {
                        username: 'admin',
                        password: 'admin'
                    },
                    // 表单验证，需要在 el-form-item 元素中增加 prop 属性
                    rules: {
                        username: [
                            {required: true, message: '账号不可为空', trigger: 'blur'}
                        ],
                        password: [
                            {required: true, message: '密码不可为空', trigger: 'blur'}
                        ]
                    },
                }
            },
            beforeMount(){
                console.log("挂载前：22222");
                console.log(this.$el);
                console.log(this.$data);
            },
            mounted(){
                /*1.页面加载完后执行*/
                /*2.非vue组件 需去掉ref后的.$el*/
                this.$refs.loginContainer.style.height = document.documentElement.clientHeight + 'px';
            },
            methods: {
                onSubmit(formName) {
                    // 为表单绑定验证功能
                    let vm = this;
                    vm.$refs[formName].validate((valid) => {
                        if (valid) {
                            $.api.login(vm.form).then(function (res) {
                                let ret = res.data;
                                if (ret.ok) {
                                    $.setUserInfo(ret.data);
                                    let redirect = vm.$route.query.redirect;
                                    if (undefined !== redirect) {
                                        redirect = decodeURI(redirect);
                                    } else {
                                        redirect = $.mainPath;
                                    }
                                    debugger;
                                    vm.$router.push(redirect);
                                } else {
                                    layer.msg(ret.msg);
                                }
                            });
                        } else {
                            return false;
                        }
                    });
                }
            }
        }
    });
</script>